//
//  HNTeacherBusinessMoneyVC.m
//  优学荟管理系统
//
//  Created by edz on 2017/11/18.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNTeacherBusinessMoneyVC.h"
#import "HNTeacherBusinessMoneyHeaderView.h"
#import "HNTeacherBusinessMoneyModel.h"
#import "HNTeacherBusinessMoneyModel.h"
#import "HNTeacherBusinessMoneyCell.h"
#import "ZYCompetitionSegmentsController.h"
@interface HNTeacherBusinessMoneyVC ()<UITableViewDelegate, UITableViewDataSource>
/** 表单 */
@property (weak, nonatomic) IBOutlet UITableView *TableView;
/** 头部视图 */
@property (nonatomic, strong) HNTeacherBusinessMoneyHeaderView *HeaderView;
@property (nonatomic, strong) NSMutableArray *DataArr;
/** 当前的页 */
@property (nonatomic, assign) NSInteger CurrentPage;
/** 临时保存的页数 */
@property (nonatomic, assign) NSInteger TempPage;
/** 开始时间 */
@property (nonatomic, strong) NSString *BeginTimeStr;
/** 结束时间 */
@property (nonatomic, strong) NSString *EndTimeStr;

@end

@implementation HNTeacherBusinessMoneyVC


#pragma mark - 生命周期
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title = @"老师业务提成";
    self.BeginTimeStr = [NSString yearMonthDay];
    self.EndTimeStr = [NSString yearMonthDay];
    // 配置TableView
    [self ConfigTableView];
    
    

    
    
}





#pragma mark - Config
/** 配置TableView */
- (void)ConfigTableView {
    // 设置代理
    self.TableView.delegate = self;
    self.TableView.dataSource = self;
    // 设置占位View
    self.TableView.tableFooterView = [UIView new];
    self.TableView.tableHeaderView = [UIView new];
    // 设置自动Cell高
    self.TableView.rowHeight = 44 * KScale;
    // 设置预注册
    [self.TableView registerNib:[UINib nibWithNibName:[HNTeacherBusinessMoneyCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    // 增加刷新头
    self.TableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 每次下拉刷新时都保存好当前的页数，预防刷新失败后页数错乱
        self.TempPage = self.CurrentPage;
        // 当前的刷新设为1
        self.CurrentPage = 1;
        // 获取数据
        [self RefreshData];
    }];
    // 增加刷新尾
    self.TableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        // 获取数据
        [self RefreshData];
    }];
}

/** 刷新失败时的页数处理 */
- (void)MgrPage {
    
    self.CurrentPage = self.TempPage;
}

/** 刷新状态 1结束刷新 2没有更多数据了 3刷新表单*/
- (void)MgrRefresh:(NSInteger)State {
    
    if (State == 1) {
        [self.TableView.mj_header endRefreshing];
        [self.TableView.mj_footer endRefreshing];
    } else if (State == 2) {
        [self.TableView.mj_footer endRefreshingWithNoMoreData];
    } else if (State == 3) {
        [self.TableView reloadData];
    } else  {
        // 无
    }
}

#pragma mark - 网络
- (void)RefreshData {
    
    NSDictionary *Params = @{
                             @"action":@"showTeaSettle",
                             @"startTime":self.BeginTimeStr,
                             @"endTime":self.EndTimeStr,
                             @"pageNumber":@(self.CurrentPage),
                             @"oid":[AccountTool shareAccountTool].currentAccount.oid
                             };
    [NetManager Get:LLBaseURL(@"resource.do") Auth:YES Params:Params Success:^(id responseObj) {
        
        // 结束刷新
        [self MgrRefresh:1];
        // 数据处理
        if ([responseObj[@"code"] isEqualToNumber:@1000]) {
            // 拿到数据
            NSArray *Arr = responseObj[@"t"][@"dataList"];
            // 如果当前的页码是1就先清空原数据
            self.CurrentPage == 1 ? [self.DataArr removeAllObjects]:nil;
            // 如果没有数据就提示没数据了
            if (Arr.count == 0) {
                
                [self MgrRefresh:2];
            } else {
                // 终于有数据啦
                [Arr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    // 处理模型转对象
                    [self.DataArr addObject:[[HNTeacherBusinessMoneyModel alloc] init_ValuForKeyWithDict:obj]];
                }];
                self.CurrentPage += 1;
                [self MgrRefresh:3];
            }
        } else {
            // 数据获取失败
            [SVProgressHUD showErrorWithStatus:responseObj[@"msg"]];
            [self MgrPage];
        }
    } Fail:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:NetErr];
        [self MgrRefresh:1];
        [self MgrPage];
    }];
}

#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.DataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HNTeacherBusinessMoneyModel *Model = self.DataArr[indexPath.row];
    HNTeacherBusinessMoneyCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.Model = Model;
    return Cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return self.HeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 211;
}


#pragma mark - Get
- (HNTeacherBusinessMoneyHeaderView *)HeaderView {
    
    if (!_HeaderView) {
        
        _HeaderView = [HNTeacherBusinessMoneyHeaderView ConfigHeaderViewOfSureAction:^(NSString *BeginTimeStr, NSString *EndTimeStr, UIButton *SureBtn) {
            
            self.BeginTimeStr = BeginTimeStr;
            self.EndTimeStr = EndTimeStr;
            [self.TableView.mj_header beginRefreshing];
        }];
    }
    return _HeaderView;
}

- (NSMutableArray *)DataArr {
    
    if (!_DataArr) {
        
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}
@end


